package com.gzsxy.esjy.request.impl;


import com.gzsxy.esjy.common.Assert;
import com.gzsxy.esjy.common.SvException;
import com.gzsxy.esjy.request.ContentType;
import com.gzsxy.esjy.request.MultipartHttpRequest;
import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.multipart.FileUpload;
import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
 * 默认文件上传请求
 * @author zhangxiaolong
 * @date 2022/7/6 9:36
 */
public class DefaultMultipartHttpRequest extends BaseRequest implements MultipartHttpRequest {

	private final HttpMethod method ;
	
	private final String uri ;
	
	private final HttpVersion protocolVersion ;
	
	private final DecoderResult decoderResult ;
	
	private final HttpHeaders headers ;
	
	private final Map<String,Object> map ;
	
	private HttpPostRequestDecoder decoder ;
	
	private final ContentType contentType ;
	
	private final Charset charset ;
	
	public DefaultMultipartHttpRequest(HttpVersion protocolVersion, HttpMethod method, String uri, HttpHeaders headers, DecoderResult result, HttpPostRequestDecoder decoder, ContentType contentType, Charset charset) {
		this.method = method ;
		this.uri = uri ;
		this.protocolVersion = protocolVersion ;
		this.decoderResult = result ;
		this.headers = headers ;
		this.map = new HashMap<String, Object>(3) ;
		this.decoder = decoder ;
		this.charset = charset ;
		this.contentType =contentType ;
	}
	
	
	@Override
	public HttpMethod getMethod() {
		return method;
	}


	@Override
	public String getUri() {
		return uri;
	}

	@Override
	public HttpVersion getProtocolVersion() {
		return protocolVersion;
	}

	@Override
	public DecoderResult getDecoderResult() {
		return decoderResult;
	}

	@Override
	public Object getParam(String name) {
		return map.get(name);
	}

	@Override
	public int getInt(String name) {
		String value = getString(name);
		try {
			return Integer.parseInt(value);
		}catch(Exception e) {
			throw SvException.getInstance(e.getMessage(), 500, e) ;
		}
	}

	@Override
	public String getString(String name) {
		Object value = map.get(name);
		return (String) value;
	}

	@Override
	public FileUpload getFile(String name) {
		Object value = map.get(name);
		Assert.notTrue(value instanceof FileUpload, String.format("value[%s] error! Cannot cast from %s to %s", name,value.getClass().getName(),FileUpload.class.getName()));
		return (FileUpload) value ;
	}

	@Override
	public Map<String, Object> getParams() {
		return map;
	}


	@Override
	public HttpHeaders getHeaders() {
		return headers;
	}


	@Override
	public String getHeader(String name) {
		return headers.get(name);
	}


	@Override
	public String getRemoteUser() {
		return super.getRemoteUser();
	}


	@Override
	public String getRemoteAddress() {
		return super.getRemoteAddress();
	}


	@Override
	public String getRequestBody() {
		return null;
	}


	@Override
	public HttpPostRequestDecoder getPostRequestDecoder() {
		return decoder;
	}


	@Override
	public ContentType getContentType() {
		return contentType;
	}


	@Override
	public Charset getCharset() {
		return charset;
	}


	@Override
	public long getContentLength() {
		return 0;
	}


}
